unit Session;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses
  ComObj, ActiveX, StdVcl, DB, DBClient,
  PoslovanjeSrv_TLB;

type
  TSession = class(TAutoObject, ISession)
  private
    FID: Integer;
    FParams: TParams;
    FUsername: WideString;
  protected
    function Get_ID: Integer; safecall;
    function Get_Username: WideString; safecall;
    procedure Set_Username(const Value: WideString); safecall;
    function Get_Data: OleVariant; safecall;
    procedure Set_Data(Value: OleVariant); safecall;
    function ISession.Get_Params = ISession_Get_Params;
    function ISession_Get_Params(const Name: WideString): OleVariant; safecall;
    procedure ISession.Set_Params = ISession_Set_Params;
    procedure ISession_Set_Params(const Name: WideString; Value: OleVariant);
      safecall;
  public
    procedure AfterConstruction; override;
    procedure BeforeDestruction; override;
  end;

var
  SessionFactory: TAutoObjectFactory;
  GlobalID: Integer;

implementation

uses ComServ;

function TSession.Get_ID: Integer;
begin
  Result := FID;
end;

function TSession.Get_Username: WideString;
begin
  Result := FUsername;
end;

procedure TSession.Set_Username(const Value: WideString);
begin
  FUsername := Value;
end;

procedure TSession.AfterConstruction;
begin
  inherited;
  Inc(GlobalID);
  FID := GlobalID;
  FParams := TParams.Create;
end;

procedure TSession.BeforeDestruction;
begin
  FParams.Free;
  inherited;
end;

function TSession.Get_Data: OleVariant;
begin
  Result := PackageParams(FParams);
end;

function TSession.ISession_Get_Params(const Name: WideString): OleVariant;
begin
  Result := FParams.ParamByName(Name).Value;
end;

procedure TSession.Set_Data(Value: OleVariant);
begin
  UnpackParams(Value, FParams);
end;

procedure TSession.ISession_Set_Params(const Name: WideString;
  Value: OleVariant);
begin
  FParams.ParamByName(Name).Value := Value;
end;

initialization
  SessionFactory := TAutoObjectFactory.Create(ComServer, TSession, Class_Session,
    ciInternal, tmApartment);
  GlobalID := 0;
end.
